<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class UserRegRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        //注意，默认是false，改成true
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|string',
            'email' => 'required|string|email|unique:users',
            'code'  => 'required|string',
            'password' => 'required|string|confirmed|min:6|max:18'
        ];
    }

    /**
     * 验证消息
     */
    public function messages()
    {
        return [
            'required'           => '字段不能为空',
            'string'             => '字段类型错误',
            'email.email'        => '邮箱格式错误',
            'email.unique'       => '该邮箱已注册',
            'password.required'  => '密码不能为空',
            'password.max'       => '密码长度不能超过18个字符',
            'password.min'       => '密码长度不能小于6个字符',
            'password.confirmed' => '两次密码不一致',
        ];
    }


    public function failedValidation(Validator $validator)
    {
        throw (new HttpResponseException(response()->json([
            'code' => 1001,
            'msg' =>  $validator->errors()->first(),
            'data' =>[],
        ], 200)));
    }

}
